layui.use(['table', 'laypage', 'layer', 'form', 'laytpl'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var flowername;
	getstock();
	$("#searchBtn").on("click", function() {
		flowername = $("#flowername").val();
		getstock();
	});
	$("#reLoad").on("click", function() {
		flowername = "";
		layer.msg("刷新", {
			time: 500
		}, function() {
			getstock();
		});
	})
	$("#delBtn").on("click", function() {
		var checkStatus = table.checkStatus('table_info');
		if(checkStatus.data.length == 0) {
			layer.alert("请至少选择一条进行删除");
			return;
		}
		var data = checkStatus.data;
		var id = new Array();
		for(var i = 0; i < data.length; i++) {
			id.push(data[i].flowerid);
		}
		deleteMore(id);
	});

	function getstock() {
		$.ajax({
			type: "post",
			url: "/flower/stock",
			async: true,
			data: {
				flowername: flowername
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_info',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'flowername',
								title: '鲜花名称',
								width: 150,
							}, {
								field: 'price',
								title: '鲜花价格',
								width: 150,
								sort: true,
							}, {
								field: 'flowersize',
								title: '鲜花单位',
								width: 100,
							}, {
								field: 'flowerproduct',
								title: '鲜花产地',
								width: 130
							}, {
								field: 'flowertype',
								title: '鲜花类型',
								width: 100
							},
							{
								field: 'flowerupstock',
								title: '库存上限',
								width: 100,
								sort: true
							},
							{
								field: 'flowerdownstock',
								title: '库存下限',
								width: 100,
								sort: true
							},
							{
								field: 'flowerinprice',
								title: '进价',
								width: 100,
								sort: true
							},
							{
								field: 'supplier',
								title: '供应商',
								width: 100
							},
							{
								field: 'count',
								title: '现有库存',
								width: 100,
								sort: true
							},
							{
								fixed: 'right',
								title: '操作',
								width: 300,
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					"data": result
				});
			}
		});
	}
	//表格里的工具栏监控
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'del') {
			deleteMore(data.flowerid);
		} else if(layEvent === 'edit') {
			detail(data.flowerid, "edit");
		} else if(layEvent == 'detail') {
			checkDetails(data.flowerid,"detail");
		}
	});

	function deleteMore(id) {
		if("" == id) {
			layer.alert("请至少选择一条信息!");
			return;
		}
		layer.confirm('确认删除此信息吗？', {
			icon: 3,
			title: '提示'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/stock/delect",
				async: true,
				data: {
					id: id
				},
				success: function(data) {
					layer.msg(data.message);
					getstock();
				}
			});
			layer.close(index);
		});
	}
	$("#addBtn").on("click", function() {
		detail(" ", "add");
	});
	//表格中的编辑事件
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '鲜花库存',
			type: 2,
			content: '/static/msg/flower/flowerstock_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '返回'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var flowername = body.find(".flowername").val();
				var flowerprice = body.find(".flowerprice").val();
				var flowersize = body.find(".flowersize").val();
				var flowerproduct = body.find(".flowerproduct").val();
				var flowertype = body.find(".flowertype").val();
				var flowerinprice = body.find(".flowerinprice").val();
				var flowerupstock = body.find(".flowerupstock").val();
				var flowerdownstock = body.find(".flowerdownstock").val();
				var supplier = body.find(".supplier").val();
				var count = body.find(".count").val();
				var isgo = true;
				if(!flowername || flowername == "" || flowername == undefined) {
					layer.msg('鲜花名称不能为空', {
						time: 2000
					});
					isgo = false;
				}
				if(isgo) {
					if(!flowerprice || flowerprice == "" || flowerprice == undefined) {
						layer.msg('用户名不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!flowersize || flowersize == "" || flowersize == undefined) {
						layer.msg('鲜花规格不能空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!flowertype || flowertype == "" || flowertype == undefined) {
						layer.msg('鲜花类型不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!flowerinprice || flowerinprice == "" || flowerinprice == undefined) {
						layer.msg('鲜花类型不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!flowerupstock || flowerupstock == "" || flowerupstock == undefined) {
						layer.msg('库存上限不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!flowerdownstock || flowerdownstock == "" || flowerdownstock == undefined) {
						layer.msg('库存下限不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!supplier || supplier == "" || supplier == undefined) {
						layer.msg('供应商不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!count || count == "" || count == undefined) {
						layer.msg('现有库存不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					tijiao();
				}
				if(type != 'detail') {

				}

				function tijiao() {
					$.ajax({
						type: "post",
						url: "/stock/" + type,
						data: {
							id: id,
							flowername: flowername,
							flowerprice: flowerprice,
							flowersize: flowersize,
							flowerproduct: flowerproduct,
							flowertype: flowertype,
							flowerinprice: flowerinprice,
							flowerupstock: flowerupstock,
							flowerdownstock: flowerdownstock,
							supplier: supplier,
							count: count
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage, {
								time: 1000
							}, function() {
								if(data.code == '1') {
									layer.close(index);
									getstock();
								}
							});
						}
					});
				}

			}
		});
	}

	function checkDetails(id,type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '公告详情',
			type: 2,
			content: '/static/msg/flower/flowerstock_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
});